package com.hntx.joymusic.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hntx.joymusic.R;


public class ExitDialog extends Dialog implements
        View.OnClickListener {
    private onBtnClickListener onBtnClickListener;

    private String sure;
    private String cancle;
    private String appTitle;
    private String tips;
    private String path;

    private Button tv_dele_sure;
    private Button tv_dele_cancle;
    private TextView tv_appTitle;
    private TextView tv_tips;

    private RelativeLayout re_tip_dialog;
    private ScaleAnimation animationStart;
    private ScaleAnimation animationEed;

    public ExitDialog(Context context, onBtnClickListener onBtnClickListener,
                      String sure, String cancle, String appTitle, String tips) {
        super(context);
        this.onBtnClickListener = onBtnClickListener;
        this.appTitle = appTitle;
        this.tips = tips;
        this.sure = sure;
        this.cancle = cancle;
    }

    public void setPath(String path) {
        this.path = path;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dele_allcontacts_dialog);
        initView();
        startAnimation();
    }

    private void initView() {
        tv_dele_sure = findViewById(R.id.sure);
        tv_dele_cancle = findViewById(R.id.cancel);
        tv_appTitle = (TextView) findViewById(R.id.appTitle);
        tv_tips = (TextView) findViewById(R.id.tips);
        re_tip_dialog = (RelativeLayout) findViewById(R.id.re_tip_dialog);

        tv_dele_cancle.setOnClickListener(this);
        tv_dele_sure.setOnClickListener(this);
        tv_dele_sure.setText(sure);
        tv_dele_cancle.setText(cancle);
        tv_appTitle.setText(appTitle);
        tv_tips.setText(tips);
    }

    public interface onBtnClickListener {
        void onSure(String path);

        void onExit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                onBtnClickListener.onExit();
                exitAnimation();
                break;
            case R.id.sure:
                    onBtnClickListener.onSure(path);
                exitAnimation();
                break;
            default:
                break;
        }
    }

    private void startAnimation() {
        if (animationStart == null) {
            animationStart = new ScaleAnimation(
                    0f, 1.0f, 0f, 1.0f,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f
            );
            animationStart.setDuration(500);
            re_tip_dialog.startAnimation(animationStart);
            Window window = this.getWindow();
            WindowManager.LayoutParams params = this.getWindow().getAttributes();
            window.setBackgroundDrawable(new BitmapDrawable());
            params.dimAmount = 0.5f;
            window.setAttributes(params);
        }
    }

    private void exitAnimation() {
        if (animationEed == null) {
            animationEed = new ScaleAnimation(1.0f, 0.0f,
                    1.0f, 0.0f, Animation.ABSOLUTE,
                    re_tip_dialog.getWidth() / 2, Animation.ABSOLUTE,
                    re_tip_dialog.getHeight() / 2);
            animationEed.setDuration(500);
            re_tip_dialog.startAnimation(animationEed);
            animListener();
        }
    }

    private void animListener() {
        if (animationEed != null)
            animationEed.setAnimationListener(new AnimationListener() {

                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    ExitDialog.this.dismiss();
                    if (animationEed != null) {
                        animationEed.cancel();
                    }
                }
            });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_ESCAPE) {
            exitAnimation();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onStop() {
        super.onStop();
        re_tip_dialog.clearAnimation();
        if (animationStart != null) {
            animationStart.cancel();
            animationStart = null;
        }
        if (animationEed != null) {
            animationEed.cancel();
            animationEed = null;
        }
    }
}